package cn.zhoutao.org.service.impl;

import cn.zhoutao.basic.service.impl.BaseServiceImpl;
import cn.zhoutao.basic.util.MD5Utils;
import cn.zhoutao.basic.util.StrUtils;
import cn.zhoutao.org.domain.Employee;
import cn.zhoutao.org.mapper.EmployeeMapper;
import cn.zhoutao.org.service.IEmployeeService;
import cn.zhoutao.user.domain.Logininfo;
import cn.zhoutao.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    //1.基础的CRUD实现通过继承BaseServiceImpl 得到

    //2.特殊的接口实现在这里自己写


    @Override
    public void save(Employee employee) {
        initEmployee(employee);
        //级联保存loininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//必须设置type
        return logininfo;
    }

    private void initEmployee(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);//引用传递不需要返回employee
    }

    @Override
    public void update(Employee employee) {
        super.update(employee);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }
}
